package com.yigang.homecontroller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.yigang.entity.PayMent;
import com.yigang.entity.Shoppingcart;
import com.yigang.entity.ShoppingcartVO;
import com.yigang.entity.User;
import com.yigang.service.PayMentService;
import com.yigang.utils.ResultUtil;
import com.yigang.utils.UserConst;

@Controller
@RequestMapping("pay")
public class PayMentController {
	@Autowired
	PayMentService payMentService;
	@RequestMapping("/Payment")
	public String payment(Model model,HttpServletRequest request, HttpSession session) {		
		Object attribute = session.getAttribute(UserConst.LOGIN_USER_INFO);
        if (null != attribute) {
            User user = JSON.parseObject((String) attribute, User.class);
            if (user != null) {
                List<Map<String, Object>> plist = payMentService.findList();		
        		System.out.println(plist);
        		model.addAttribute("plist", plist);
                
        		return "jsp/home/payment";
            }
            return "jsp/userLogin";
        }
        return "jsp/userLogin";
    }
	@RequestMapping("/addCart")
    @ResponseBody
    public ResultUtil addCart(PayMent payMent, HttpServletRequest request, HttpSession session) {
        Object attribute = session.getAttribute(UserConst.LOGIN_USER_INFO);
        if (null != attribute) {
            User user = JSON.parseObject((String) attribute, User.class);
           
            
            
            int i = payMentService.addCart(payMent);
             if (i > 0) {
                return ResultUtil.ok(1);
            } else {
                return ResultUtil.error("订单创建失败");
            }
        } else {
            return ResultUtil.error("请先登录");
        }
    }
}
